/**
 * @file event.c
 * @author taylor peng
 * @brief 事件处理
 * @version 0.1
 * @date 2022-01-08
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include "event.h"
#include "bitmap.h"
#include <stdbool.h>
#include <string.h>
#include <Arduino.h>
#include <Ticker.h>

/** 睡眠参数 */
static unsigned char SleepEvent = false;
/** 休眠后屏保延迟显示标志 */
static unsigned char SleepScreenProtectFlag = false;
/** 欠压参数 */
static bool UnderVoltageEvent = false;
/** 爆发模式参数 */
static bool BoostEvent = false;
/** 蓝牙参数 */
static unsigned char BLE_State = true;
/** 温度状态参数 */
static unsigned char TempCTRL_Status = TEMP_STATUS_OFF;
/** 温度差参数 */
static double TempGap = 0;
/** 假温度参数 */
static double PID_Setpoint = 0;
/** 真实温度 */
static double TipTemperature = 0;
/** 角标事件更新 */
static unsigned int pages_Tip_Display_timer = 0;
/** MAC */
static unsigned long long ChipMAC;
static char ChipMAC_S[19] = {0};
/** 编译时间 */
static char CompileTime[20];
/** 烙铁头数目 */
static unsigned char TipTotal = 1;
/** 烙铁头调用休眠事件 */
static unsigned char TipCallSleepEvent = false;
/** 烙铁头配置信息 */
TIPCONFIG_S MyTip[MaxTipConfig] = 
{
    {
        {51.6128997803, 0.1443523616, 0.0000061750, -0.0000000001},
        {
            {30.0, 0, 0.5},
            {20.0, 0, 0.5}
        },
        "智能加热台"
    },
};
const char* TempCTRL_Status_Mes[] = {
    "错误",
    "停机",
    "休眠",
    "提温",
    "正常",
    "加热",
    "维持",
};

/** 按键事件 */
static unsigned char SYSKey = 0;
/** 功率事件 */
static unsigned short POWER = 0;
/** 爆发计数 */
static unsigned int BoostTimer = 0;
/** 爆发模式持续时间      (秒) */
static unsigned int BoostTime = 60;
/** 事件计时器 */
static unsigned int EventTimerUpdate = 0;
/** 屏保事件计时器 */
static unsigned int ScreenProtectorTimer = 0;
/** 关机提醒 */
static unsigned int ShutdownTime = 0;
/** 关机事件 */
static unsigned char ShutdownEvent = 0;
/** 休眠触发时间 */
static unsigned int SleepTime = 10;      //休眠触发时间          (分)
/** 屏保在休眠后的触发时间(秒) */
static unsigned int ScreenProtectorTime = 60;
/** 编码器锁定标志 */
static unsigned char Counter_LOCK_Flag = false;

/**
 * @brief 休眠事件
 * 
 */
void Sleep_SetEvent(unsigned char state)
{
    SleepEvent = state;
}

unsigned char Sleep_GetEvent(void)
{
    return SleepEvent;
}

/**
 * @brief 休眠触发时间
 * 
 */
void SleepTrigerTime_Set(unsigned int tick)
{
    SleepTime = tick;
}

unsigned int SleepTrigerTime_Get(void)
{
    return SleepTime;
}

/**
 * @brief 休眠后屏保延迟显示标志
 * 
 */
void SleepScreenProtect_Set(unsigned char state)
{
    SleepScreenProtectFlag = state;
}

unsigned char SleepScreenProtect_Get(void)
{
    return SleepScreenProtectFlag;
}

/**
 * @brief 屏保事件计时器
 * 
 */
void ScreenProtectorTimerUpdate_Set(unsigned int tick)
{
    ScreenProtectorTimer = tick;
}

unsigned int ScreenProtectorTimerUpdate_Get(void)
{
    return ScreenProtectorTimer;
}

/**
 * @brief 屏保在休眠后的触发时间(秒)
 * 
 */
void ScreenProtectorTrigerTime_Set(unsigned int tick)
{
    ScreenProtectorTime = tick;
}

unsigned int ScreenProtectorTrigerTime_Get(void)
{
    return ScreenProtectorTime;
}

/**
 * @brief 低电压事件
 * 
 */
void UnderVoltage_Set(unsigned char state)
{
    UnderVoltageEvent = state;
}

unsigned char UnderVoltage_Get(void)
{
    return UnderVoltageEvent;
}

/**
 * @brief 爆发模式，加热事件
 * 
 */
void BoostEvent_Set(unsigned char state)
{
    BoostEvent = state;
}

unsigned char BoostEvent_Get(void)
{
    return BoostEvent;
}

/**
 * @brief 爆发事件
 * 
 */
void BoostTimer_Set(unsigned int tick)
{
    BoostTimer = tick;
}

unsigned int BoostTimer_Get(void)
{
    return BoostTimer;
}

/**
 * @brief 爆发模式持续时间
 * 
 */
void BoostDuringTime_Set(unsigned int tick)
{
    BoostTime = tick; 
}

unsigned int BoostDuringTime_Get(void)
{
    return BoostTime;
}

/**
 * @brief 关机提醒
 * 
 */
void ShutdownTime_Set(unsigned int minute)
{
    ShutdownTime = minute;
}

unsigned int ShutdownTime_Get(void)
{
    return ShutdownTime;
}

/**
 * @brief 关机事件
 * 
 */
void ShutdownEvent_Set(unsigned char state)
{
    ShutdownEvent = state;
}

unsigned char ShutdownEvent_Get(void)
{
    return ShutdownEvent;
}

/**
 * @brief 错误事件
 * 
 */


/**
 * @brief 蓝牙事件
 * 
 */
void BluetoothEvent_Set(unsigned char state)
{
    BLE_State = state;
}

unsigned char BluetoothEvent_Get(void)
{
    return BLE_State;
}

/**
 * @brief 温度控制状态事件
 * 
 */
void TemperatureCtrlEvent_Set(unsigned char state)
{
    TempCTRL_Status = state;
}

unsigned char TemperatureCtrlEvent_Get(void)
{
    return TempCTRL_Status;
}

/**
 * @brief 温度差事件
 * 
 */
void TemperatureDiffEvent_Set(double temp_diff)
{
    TempGap = temp_diff;
}

double TemperatureDiffEvent_Get(void)
{
    return TempGap;
}

/**
 * @brief 假温度事件
 * 
 */
void TemperatureVirtualEvent_Set(double virtual_temp)
{
    PID_Setpoint = virtual_temp;
}

double TemperatureVirtualEvent_Get(void)
{
    return PID_Setpoint;
}

/**
 * @brief 真实温度事件
 * 
 */
void TemperatureRealityEvent_Set(double reality_temp)
{
    TipTemperature = reality_temp;
}

double TemperatureRealityEvent_Get(void)
{
    return TipTemperature;
}

/**
 * @brief 角标事件更新，控制右下角角标显示时间
 * 
 */
void PagesCornerTipEvent_Set(unsigned int tick)
{
    pages_Tip_Display_timer = tick;
}

unsigned int PagesCornerTipEvent_Get(void)
{
    return pages_Tip_Display_timer;
}

/**
 * @brief MAC事件
 * 
 */
void Mac_Set(void)
{
    //获取系统信息
    ChipMAC = ESP.getEfuseMac();
    sprintf(CompileTime, "%s %s", __DATE__, __TIME__);
    for (unsigned char i = 0; i < 6; i++)
    {
        sprintf(ChipMAC_S + i * 3, "%02X%s", ((unsigned char*)&ChipMAC)[i], (i != 5) ? ":" : "");
    }
}

char *Mac_GetString(void)
{
    return ChipMAC_S;
}

/**
 * @brief CPU频率事件
 * 
 */
unsigned long Cpu_GetFreq(void)
{
    return ESP.getCpuFreqMHz();
}

/**
 * @brief Version事件
 * 
 */
char *Version_Get(void)
{
    // return ESP.getSdkVersion();
    return 0;
}

/**
 * @brief 烙铁头数目事件
 * 
 */
void TipNum_Set(unsigned char num)
{
    TipTotal = num;
}

unsigned char TipNum_Get(void)
{
    return TipTotal;
}

/**
 * @brief 烙铁头休眠事件
 * 
 */
void TipCallSleepEvent_Set(unsigned char state)
{
    TipCallSleepEvent = state;
}

unsigned char TipCallSleepEvent_Get(void)
{
    return TipCallSleepEvent;
}

/**
 * @brief 烙铁头配置信息事件
 * 
 */
void TipConfigTemperature_Set(unsigned char tip_id, double temp[])
{
    for (unsigned char i = 0; i < 4; ++i)
    {
        MyTip[tip_id].PTemp[i] = temp[i];
    }
}

double *TipConfigTemperature_Get(unsigned char tip_id)
{
    return MyTip[tip_id].PTemp;
}

void TipConfigPid_Set(unsigned char tip_id, float *pid, unsigned char row, unsigned char col)
{
    for (unsigned char i = 0; i < row; ++i)
    {
        for (unsigned char j = 0; j < col; ++j)
        {
            MyTip[tip_id].PID[i][j] = *(pid + i * col + j);
        }
    }
}

float *TipConfigPid_Get(unsigned char tip_id)
{
    return &MyTip[tip_id].PID[0][0];
}

void TipConfigName_Set(unsigned char tip_id, const unsigned char *name)
{
    int i = 0;
    while (*name != '\0')
    {
        MyTip[tip_id].name[i] = *name++;
        i++;
    }
}

unsigned char *TipConfigName_Get(unsigned char tip_id)
{
    return MyTip[tip_id].name;
}

/**
 * @brief 按键事件
 * 
 */
void KeyEvent_Set(unsigned char key_type)
{
    SYSKey = key_type;
}

unsigned char KeyEvent_Get(void)
{
    return SYSKey;
}

/**
 * @brief 功率事件
 * 
 */
void Power_Set(unsigned short power)
{
    POWER = power;
}

unsigned short Power_Get(void)
{
    return POWER;
}

/**
 * @brief 单击功率爆发技能 事件
 * 
 */
void BoostButton_EventLoop(void)
{
    //单击 进行短时功率爆发
    if (KeyEvent_Get() == EVT_ONE_CLICK)
    {
        if (!BoostEvent_Get())
        {
            BoostEvent_Set(true);
            BoostTimer_Set(millis());
        }
        else
        {
            BoostEvent_Set(false);
        }
    }
    //功率爆发技能计时器
    if (millis() - BoostTimer_Get() > BoostDuringTime_Get() * 1000)
    {
        //技能时间到
        BoostEvent_Set(false);
    }
}

/**
 * @brief 事件计时器
 * 
 * @param tick 
 */
void EventTimerUpdate_Set(unsigned int tick)
{
    EventTimerUpdate = tick;
}

unsigned int EventTimerUpdate_Get(void)
{
    return EventTimerUpdate;
}

/**
 * @brief 编码器锁定
 * 
 */
void LockState_Set(unsigned char state)
{
    Counter_LOCK_Flag = state;
}

unsigned char LockState_Get(void)
{
    return Counter_LOCK_Flag;
}

/**
 * @brief 定时器更新事件
 * 
 */
void TimerUpdateEvent(void)
{
    EventTimerUpdate_Set(millis());
    ScreenProtectorTimerUpdate_Set(millis());
    PagesCornerTipEvent_Set(EventTimerUpdate_Get());
}

/**
 * @description: 计时器事件Loop
 * @param {*}
 * @return {*}
 */
void TimerEventLoop(void)
{
    //事件事件距离计时器
    unsigned int TimerEventTimer = millis() - EventTimerUpdate_Get();

    //更新BOOST提温事件
    BoostButton_EventLoop();
    //更新PID菜单快速召唤事件
    // FastPID_MenuSummon_EventLoop();

    //刷新手柄触发开关事件：可引发手柄休眠事件
    // SW_WakeLOOP();

    //更新休眠和停机时间    停机优先级 > 休眠优先级
    if (ShutdownTime_Get() != 0 && TimerEventTimer > Minute2Millis(ShutdownTime_Get()))
    {
        //系统停机
        ShutdownEvent_Set(true);
        Sleep_SetEvent(false);
    }
    else if (TipCallSleepEvent_Get() || SleepTrigerTime_Get() != 0 && TimerEventTimer > Minute2Millis(SleepTrigerTime_Get()))
    {
        //进入休眠
        Sleep_SetEvent(true);
        //锁定编码器，因为休眠模式中不允许修改设定温度
        LockState_Set(true);
    }
    else
    {
        if (Sleep_GetEvent())
        {
            Sleep_SetEvent(false);
            LockState_Set(false);
        }
        ShutdownEvent_Set(false);
        //不休眠\不停机 则重置屏保计时器，确保不会进入屏保
        ScreenProtectorTimerUpdate_Set(millis());
    }

    //休眠超过一段时间后进入屏保程序
    if (ScreenProtectorTrigerTime_Get() != 0 && millis() - ScreenProtectorTimerUpdate_Get() > ScreenProtectorTrigerTime_Get() * 1000)
    {
        //开启屏保程序
        SleepScreenProtect_Set(true);
    }
    else
    {
        SleepScreenProtect_Set(false);
    }
}

/*** 
 * @description: 分钟转毫秒
 * @param {uint32_t} min
 * @return {*}
 */
unsigned int Minute2Millis(float min)
{
    return min * 60 * 1000;
}
